
package net.phoenix.repository.nodetype;

import java.util.LinkedHashMap;
import java.util.Map;

import javax.jcr.nodetype.NodeDefinition;
import javax.jcr.nodetype.PropertyDefinition;


import net.phoenix.repository.data.NodeDefinitionData;
import net.phoenix.repository.data.NodeTypeData;
import net.phoenix.repository.data.PropertyDefinitionData;


/**
* Jigsaw server
* <p> net.phoenix.repository.definition</p>
* <p> XMLNodeDefinition</p>
* 
* @author    <a href="shamphone@gmail.com">Li XiongFeng</a>
* @date      2011-11-26
* @version   1.0.0
*/
public class XMLNodeType extends GeneralNodeType {
	private XMLNodeTypeDefinitionManager manager;
	private NodeTypeData data;
	private LinkedHashMap<String, PropertyDefinition> _declaredProperties;
	private LinkedHashMap<String, NodeDefinition> _declaredChildren;
	public XMLNodeType(XMLNodeTypeDefinitionManager manager,NodeTypeData data){
		super(manager);
		this.manager = manager;
		this.data = data;
		this._declaredProperties = null;
		this._declaredChildren = null;
		
	}


	@Override
	public String getName() {
		return data.getName();
	}



	@Override
	protected synchronized Map<String, PropertyDefinition> loadDeclaredPropertyDefinitions() {
		if(this._declaredProperties!=null)
			return this._declaredProperties;
		this._declaredProperties = new LinkedHashMap<String, PropertyDefinition>();
		for(PropertyDefinitionData property: this.data.getPropertyDefinitions()){
			PropertyDefinition item = new XMLPropertyDefinition(manager, this, property);
			_declaredProperties.put(item.getName(), item);
		}
		return _declaredProperties;
	}

	@Override
	protected Map<String, NodeDefinition> loadDeclaredNodeDefinitions() {
		if(this._declaredChildren!=null)
			return this._declaredChildren;
		this._declaredChildren = new LinkedHashMap<String, NodeDefinition>();
		for(NodeDefinitionData child: this.data.getNodeDefinitions()){
			NodeDefinition item = new XMLNodeDefinition(manager, this, child);
			_declaredChildren.put(item.getName(), item);
		}
		return _declaredChildren;
	}


	@Override
	public String[] getDeclaredSupertypeNames() {	
		return  this.data.getSuperTypes();		
	}


	@Override
	public boolean isAbstract() {
	return this.data.isAbstract();
	}


	@Override
	public boolean isMixin() {
		return this.data.isMixin();
	}


	@Override
	public boolean hasOrderableChildNodes() {
		return this.data.isHasOrderableChildNodes();
	}


	@Override
	public boolean isQueryable() {		
		return this.data.isQueryable();
	}


	@Override
	public String getPrimaryItemName() {
		return this.data.getPrimaryItemName();
	}


	@Override
	public PropertyDefinition[] getDeclaredPropertyDefinitions() {
		Map<String, PropertyDefinition> properties = this.loadDeclaredPropertyDefinitions();
		return properties.values().toArray(new PropertyDefinition[properties.size()]);
	}


	@Override
	public NodeDefinition[] getDeclaredChildNodeDefinitions() {
		Map<String, NodeDefinition> children = this.loadDeclaredNodeDefinitions();
		return children.values().toArray(new NodeDefinition[children.size()]);
	}






}
